You Can Write Perl in Any Language, Too

Mark Leighton Fisher on 2007-12-07T18:05:08

It is said that "You can write FORTRAN in any language." I say that you can write (within limits) any language in any other language, although writing Common LISP in FORTRAN IV would probably involve first writing a Common LISP interpreter in FORTRAN IV.

As an example, let's take humble VB6. A common Perl idiom is to pass around a set of strings as one string concatenated by join() and de-concatenated by split(). Larry Wall borrowed join() and split() from Basic to start with, then enhanced split() to split on regexes (among other improvements). But non-regex join() and split() work just fine in VB6:

    tokenStr = join(passwd, ":")
    [...]
    tokens = split(tokenStr, ":")

And quite a bit of the time, you don't need regex-based split() and join() — ordinary split() and join() work just fine. (Maybe it is just me, but I haven't seen the join()/split() idiom much in native Basic code of whatever stripe.)

"But what about regexes?" you ask. In Win32 land (as I am), you can use the (essentially-built-in) Microsoft VBScript Regular Expressions 5.5. Though not as convenient as Perl's regexes, these VBScript 5.5 Regular Expressions are about as convenient as you can make class-based regexes:

    dim loincMatch as RegExp
    Set loincMatch = New RegExp
    ...
    loincMatch.Pattern = "^\d{1,5}-\d$"
    if loincMatch.Test(field(4)) Then
    ...

Everywhere (there is a C compiler) there are PCRE (Perl-Compatible Regular Expressions), which truly is about as close as you can get to Perl regexes outside of a Perl program (or plug-in).

Control structures from other languages can be used too (think of RATFOR for FORTRAN 66 or Switch in Perl 5). Fortunately, most modern languages have a fairly decent set of control structures.

This is part of the meaning of a Turing-complete language — anything that can be expressed in one programming language can be expressed in another programming language, although (as I have noted before) you may end up writing an interpreter for language A in language B before you can start using A's language constructs when writing language B.


surprised by language borrowing

mr_bean on 2007-12-08T01:43:34

I knew about PCRE, but didn't know split, join
came from BASIC.

The way language designers 'port' other languages'
features could be called 'incestuous.'

But that's not really the right word.

Evolutionarily speaking, it's not 'inheritance
of acquired features' but 'inheritance of other
monkeys' cool features.'

Re:surprised by language borrowing

Aristotle on 2007-12-08T09:57:08

It’s not even inheritance. It’s more like bacterial conjugation.

bacterial conjugation

mr_bean on 2007-12-14T02:28:01

I had to look that up. Here's an animation

I think you're right. I wonder if perl6 will change that.

The importance of BASIC to Larry comes out in the Onion State

Re:bacterial conjugation

Mark Leighton Fisher on 2007-12-14T17:48:37

I think Perl 6 will change the game because it will be the first widely-deployed non-LISP-based language with many cool features now present only in academic languages. I suspect that we will see other languages start borrowing from Perl 6 because of these cool features.